<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<?php 
	$USER = getAccountUserObject();
	
	$listAllThreads = $this->escort_message_thread_entity->listAllThreadMessages($USER->getId(),USER_TYPE_AGENT);	 
	$countUnread = $this->escort_message_thread_entity->countAllUnreadMessagesOfUser($USER->getId(),USER_TYPE_AGENT);	
	
	$current_dbprefix = $this->db->dbprefix;
	$this->db->set_dbprefix('default_');
	
	$page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0 ;
	$records_per_page = 100000000; 
	$total = count($listAllThreads);
			
	$pagination = create_pagination( 
								$uri = 'escorts/async/loadAllThreads?', 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	$records = $this->db->where(array("escort_id"=>$USER->getId(), "origin"=>USER_TYPE_AGENT)) 
			->order_by('created_time','desc')->limit($records_per_page, $page )->get(TBL_MESSAGE_THREAD)->result();
			
	$this->db->set_dbprefix($current_dbprefix);	
?>

<?php foreach($records as $item): ?>
	<?php 
		$featureMessage = $this->escort_message_thread_entity->getFeatureMessageOfThread($item->id); 
		$unreadMessages = $this->escort_message_thread_entity->countAllUnReadMessage($item->id);
		$thread_id = $item->id;
	?>
	<?php if($featureMessage):?>
		<?php 
			$read_class = ($featureMessage->read == 1) ? 'read':'unread'; 
			$title = ($featureMessage->read == 1) ? language_translate('support_label_title_mark_unread'):language_translate('support_label_title_mark_read'); 
			$css_class = ($unreadMessages>0) ? 'unread_conversation':'';
		?>
		<div class="dashboard-info-right thread-item read-thread-item <?php echo $css_class;?> message_item_<?php echo $featureMessage->id;?>" rel="<?php echo $item->id;?>" id="conversation_<?php echo $item->id;?>">
			<div class="item-name"><?php echo $featureMessage->sender;?></div>
			<div class="clear"></div>
			<div class="item-message">
				<?php echo $featureMessage->message;?>
			</div>	
			<div class="item-info-right">
				<div class="item-date"><?php echo sysDateFormat($featureMessage->sent_time,DATE_FORMAT);?></div>
				<div class="item-read-unread mark-item <?php echo $read_class;?>" title="<?php echo $title;?>" rel="<?php echo $featureMessage->id;?>" ></div>	
				<div class="item-delete delete delete_thread_<?php echo $thread_id;?> is_thread" title="<?php echo language_translate('support_label_title_delete');?>" rel="<?php echo $featureMessage->id;?>" ></div>	
			</div>	
			<div class="item-number-unread" id="unread_message_item_<?php echo $thread_id?>">
				<?php 
					echo $this->escort_message_thread_entity->displayUnreadMessage($thread_id);
					//if($unreadMessages) echo "( <span id='unread_thread_$thread_id' class='unread-thread'>".$unreadMessages.'</span> '.language_translate('support_new_unread_messages').' )';
				?>
			</div>
			<div class="clear"></div>
			<div class="thread_conversation"></div>
		</div>
		<div class="clear"></div>
	<?php endif;?>	
<?php endforeach;?>
<div class="clear"></div>
<div class="pagging">
	<div id="browse_threads">
		<?php echo $pagination['links'];?>
	</div>
</div>	

<script type="text/javascript">
	var UNREAD = <?php echo $countUnread;?>;
	var MODE = 'thread';
</script>
